package com.ncloud.product.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.chnl.service.ChnlService;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.common.utils.RedisUtils;
import com.ncloud.product.common.Constant;
import com.ncloud.product.entity.ChnlDiscount;
import com.ncloud.product.entity.Discnt;
import com.ncloud.product.entity.DiscntPrice;
import com.ncloud.product.entity.Product;
import com.ncloud.product.mapper.ChnlDiscountMapper;
import com.ncloud.product.mapper.DiscntMapper;
import com.ncloud.product.mapper.ProductMapper;
import com.ncloud.product.service.ProductService;

@Service
public class ProductServiceImpl implements ProductService{
	
	@Autowired
	private ProductMapper prodMapper;
	@Autowired
	private DiscntPriceServiceImpl priceService;
	@Autowired
	private ChnlDiscountMapper chnlDiscountMapper;
	@Reference
	private ChnlService chnlService;
	@Autowired
	private DiscntMapper discntMapper;
	@Autowired
	private RedisUtils redis;
	private static final Long expireTime= 7*24*60*60L;

	@Override
	public JSONObject getProdInfo(JSONObject json) {
		if(!json.containsKey("prodId")){
			throw new UserException("2001","缺少参数");
		}
		Long prodId = json.getLong("prodId");
		JSONObject result = new JSONObject();
		if(redis.exists("PRODUCT_INFO_"+prodId)){
			result = (JSONObject) redis.get("PRODUCT_INFO_"+prodId);
		}else {
			Product prod = prodMapper.getProdInfo(prodId);
			result = JSON.parseObject(JSON.toJSONString(prod));
			redis.set("PRODUCT_INFO_"+prodId, result, expireTime);
		}
		return result;
	}

	@Override
	public JSONObject getProdPriceInfo(JSONObject json) {
		if(!json.containsKey("discntId")){
			throw new UserException("2001","缺少参数");
		}
		if(json.getLong("prodId")==null) {
			Long discntId = json.getLong("discntId");
			Discnt discnt = discntMapper.getDiscntById(discntId);
			json.put("prodId", discnt.getProdId());
		}
		Long priceId = priceService.getPriceByItems(json);
		DiscntPrice price = priceService.getByPriceId(priceId);
		//获取渠道折扣信息
		Long userId = json.getLong("userID");
		JSONObject chnlJson = chnlService.getChnlByUserId(userId);
		ChnlDiscount chnlDiscount = null;
		if(chnlJson!=null && !chnlJson.isEmpty()){
			chnlDiscount = chnlDiscountMapper.getDiscountByChnlId(chnlJson.getLong("chnlId"),Constant.CHNL_DISCOUNT_OBJECT_PRICE,price.getPriceId() );
		}
		JSONObject jsonObject = calculateProdFeeScope(json, price,chnlDiscount);
		
		return jsonObject;
	}
	
	
	private JSONObject calculateProdFeeScope(JSONObject json, DiscntPrice price, ChnlDiscount chnlDiscount) {
		//计算费用信息(费用*折扣*速率*时长)
		Long billingCycle = 1L;
		Long bandWidth = 1L;
		if(Constant.DISCNT_PRICE_FEE_UNIT_TYPE_MONTHLY.equals(price.getFeeUnitType())){
			if(json.containsKey("billingCycle")){
				billingCycle = json.getLong("billingCycle");
			}
		}else if (Constant.DISCNT_PRICE_FEE_UNIT_TYPE_MONTHLY_EVERYM.equals(price.getFeeUnitType())) {
			if(json.containsKey("billingCycle")){
				billingCycle = json.getLong("billingCycle");
			}
			if(json.containsKey("bandWidth")){
				bandWidth = json.getLong("bandWidth");
			}
		}
		Long baseFee = price.getFeeValue()*billingCycle*bandWidth;
		Long minFee = baseFee;
		if(chnlDiscount!=null){
			if(Constant.CHNL_DISCOUNT_TYPE_DISCNT.equals(chnlDiscount.getDiscountType())){
				//折扣比例
				minFee = baseFee*chnlDiscount.getDiscount()/100;
			}else if(Constant.CHNL_DISCOUNT_TYPE_PRICE.equals(chnlDiscount.getDiscountType())){
				//固定额度
				minFee = chnlDiscount.getPrice();
			}else{
				//待扩充
			}
		}
		//Long bandwidth = json.getLong("bandWidth");
		JSONObject result = new JSONObject();
		
		result.put("minFee", minFee); //返回前台为分
		result.put("maxFee", baseFee);
		return result;
	}

	@Override
	public JSONArray getProdList(JSONObject json) {
		// 判断参数
		if(!json.containsKey("userID"))
			throw new UserException(ResultEnum.PARAM_ERROR);
		Long userId = json.getLong("userID");
		JSONObject chnl = chnlService.getChnlByUserId(userId);
		Long chnlId = chnl.getLong("chnlId");
		JSONArray result = new JSONArray();
		//放入redis key=PRODUCT_RELEASE_CHNL_{chnlId}
		if(redis.exists("PRODUCT_RELEASE_CHNL_"+chnlId)){
			result = (JSONArray) redis.get("PRODUCT_RELEASE_CHNL_"+chnlId);
		}else{
			List<Product> products = prodMapper.getProdList(chnl.getLong("chnlId"));
			result = JSON.parseArray(JSON.toJSONString(products));
			if(products.size()>0){
				redis.set("PRODUCT_RELEASE_CHNL_"+chnlId, result, expireTime);
			}
		}
		
		return result;
	}

}
